//
//  ZBHomeViewController.m
//  XZBProduct
//
//  Created by xzb on 2018/7/2.
//  Copyright © 2018年 xzb. All rights reserved.
//

#import "ZBHomeViewController.h"

#import "ZBHomeRequest.h"

@interface ZBHomeViewController ()

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) ZBHomeRequest *homeRequest;
@property (nonatomic, strong) ZBHomeModel *homeModel;

@end

@implementation ZBHomeViewController

#pragma mark - life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"上班族便当";
}

#pragma mark - api

#pragma mark - Delegete

#pragma mark - Event Response

#pragma mark - privite

- (void)ZB_addSubView
{
    [super ZB_addSubView];
    
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(self.navigationBarBottom);
        make.left.right.offset(0);
        make.bottom.offset(0);
    }];
}

- (void)ZB_sendQuery
{
    [super ZB_sendQuery];
    [self.homeRequest sendQuery];
}

- (void)updateData
{
    if (!self.homeModel || ![self.homeModel isKindOfClass:[ZBHomeModel class]]) {
        return;
    }
    
    [self.collectionView zb_setup:^(ZBCollectionMaker *maker) {
        maker.section(@"<section-menu");
        for (NSDictionary *item in self.homeModel.items) {
            maker.row(@"<cell-item>").model(item);
        }
    }];
}

#pragma mark - delegete

#pragma mark - notification

#pragma mark - kvo

#pragma mark - getter And setter

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 10;
        layout.minimumLineSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
        
        _collectionView = [UICollectionView collectionViewWithLayout:layout];
        
        @weakify(self);
        [self.collectionView zb_blackHeaderRefreshingBlcok:^{
            @strongify(self);
            [self ZB_sendQuery];
        }];
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            @strongify(self);
            [self.collectionView.mj_footer endRefreshing];
            [self.collectionView.mj_footer resetNoMoreData];
        }];
        [footer setTitle:@"已经到底了" forState:1];
        self.collectionView.mj_footer = footer;
        
        [_collectionView zb_configTableView:^(ZBCollectionProtoFactory *config) {
            config.cell(@"<cell-item>").cls(@"ZBHomeItemCell").nibName(@"ZBHomeItemCell");
            config.cell(@"<cell-space>").cls(@"ZBSpaceCell");
        }];
    }
    return _collectionView;
}

- (ZBHomeRequest *)homeRequest
{
    if (!_homeRequest){
        _homeRequest = [[ZBHomeRequest alloc] init];
        @weakify(self);
        _homeRequest.onCompletion = ^(id result) {
            @strongify(self);
            if ([self.collectionView.mj_header isRefreshing]) {
                [self.collectionView.mj_header endRefreshing];
            }
            if ([self.collectionView.mj_footer isRefreshing]) {
                [self.collectionView.mj_footer endRefreshing];
            }
            self.homeModel = result;
            [self updateData];
        };
        _homeRequest.onError = ^(id error) {
            @strongify(self);
            if ([self.collectionView.mj_header isRefreshing]) {
                [self.collectionView.mj_header endRefreshing];
            }
            if ([self.collectionView.mj_footer isRefreshing]) {
                [self.collectionView.mj_footer endRefreshing];
            }
            self.homeModel = nil;
            [self updateData];
        };
    }
    return _homeRequest;
}
@end
